#include "stdafx.h"
#include "fcgi_server_t.h"

std::string DB_NAME; // recently it aws const, rename...
graph_virtual::global_data_t graph_virtual::global::global_;
fcgi_server_ptr srv;

void user_input()
{
    std::cout << "type exit when needed" << std::endl;
    std::string input;
    while(true)
    {
        std::cin >> input;
        if (input.find("exit") != std::string::npos)
        {
            std::cout << "the server will stop after processing of first new request" << std::endl;
            srv->stop();
            break;
        }
    }
}

void init_graph()
{
   std::cout << "Deserializing..." << std::endl;
   graph_virtual::virtual_graph_ptr_t graph = 
      graph_virtual::virtual_graph_ptr_t(new graph_virtual::virtual_graph_t(DB_NAME, 100));
   graph_virtual::global::get_instance().set_new_graph(graph);
   std::cout << "Ended initialization of the service..." << std::endl;
}

void release_graph()
{
   //do nothing hear - usin shared_ptr
}

int main(int argc, char** argv) 
{
   int port=10001;
   DB_NAME = "osm.db";
   if(argc > 1)
      port = boost::lexical_cast<int>(argv[1]);
   if(argc > 2)
      DB_NAME = std::string(argv[2]);

   //user command reader thread
   boost::thread usr_thread(&user_input);

   init_graph();
   srv  = fcgi_server_ptr(new fcgi_server_t(port, std::cerr));
   int res = srv->run_sync();
   release_graph();
   return res;
}
